package com.hinner.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hinner.entity.SysMenu;
import com.hinner.mapper.SysMenuMapper;
import com.hinner.mapper.SysRoleMapper;
import com.hinner.service.SysMenuService;
import com.hinner.service.SysRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    /**
     * 获取菜单树
     * @param sysMenuList
     * @return
     */
    @Override
    public List<SysMenu> buildMenuTree(List<SysMenu> sysMenuList) {
        List<SysMenu> list = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            // 子节点 : 就是再遍历一边，和父层节点比较
            for (SysMenu menu : sysMenuList) {
                if (Objects.equals(menu.getParentId(), sysMenu.getId())){
                    sysMenu.getChildMenus().add(menu);
                }
            }

            // 父节点
            if (sysMenu.getParentId() == 0L) {
                list.add(sysMenu);
            }
        }
        return list;
    }
}
